package com.communal.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.communal.service.IModelService;
import com.communal.util.AppException;
import com.communal.util.CommunalFilterBean;

/**
 * Controller for handle all action for model <br>
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Controller
public class ModelController extends AbstractController{

	private final String VIEW_ACTION = "/general/view.action";
	
	@Autowired
	IModelService modelService;
	
	@RequestMapping(value=VIEW_ACTION)
	public @ResponseBody Map<String, ? extends Object> view(
			@RequestParam (value = "year", required = false) Short year,
			@RequestParam (value = "month", required = false) Short month,
			@RequestParam (value = "dir", required = false ) String dir,
			@RequestParam (value = "sort", required = false ) String sort){
		try{
			return getMap( modelService.getTotalInformation(  
					userService.getModelForCurrentLoggedUser(), new CommunalFilterBean( year, month, null, null ) ) );
		} catch (AppException error) {
			return getModelMapError(error.getMessage());
		}
	}
}
